<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="item.boxCode">
					<uni-card :title="'序号:'+(index+1)" v-if='!item.flag'>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
					
					<uni-card :title="'序号:'+(index+1)" v-else style='background-color: darkgrey;'>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
				
			</scroll-view>
		</view>
		
		<view class="footer">
			<button type="default" size='mini' @click="refuse" style="width:48%; margin-left:1%;">拒绝</button>
			<button type="default" size='mini' @click="submit" style="width:48%; margin-left:1%;">同意</button>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue';
	import { getToken } from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var globalEvent = uni.requireNativePlugin('globalEvent');
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				productList:[],
				userflag:false,
				code:'',
				content:'',
				codeEqualQty:'',
				boxCode:'',
				count:0,
				content:'',
				reduceContent:'',
				reduceCode:'',
				reduceNumkber:'',
				reducePatch:'',
				reduceCount:0,
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				orgId:getApp().globalData.config.orgId,
				pkOrder:'',
				status:'',
				title:'',
				id:'',
				index:'',
			}
		},
		
		created() {	
			this.orgId = uni.getStorageSync('orgId');
			this.queryData();
			// scanDevice = new ScanDeviceClass();
			// scanDevice.openScan(); // 打开扫描
			// scanDevice.setOutScanMode(0); // 扫描模式		
			// this.initScan();
			// this.registerScan();
		},
		
		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
			     return false;
			}
			//that.unregisterScan();
			let pages = getCurrentPages();  //获取所有页面栈实例列表
			let nowPage = pages[ pages.length - 1];  //当前页页面实例
			let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
			prevPage.$vm.chooseTab(that.index);
			uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
			    delta: 1
			});
			return true;
		},
		
		// onHide: function() {
		// 	this.unregisterScan();
		// },
		// destroyed: function() {
		// 	this.unregisterScan();
		// },
		
		onLoad: function (option){
			this.orgId = uni.getStorageSync('orgId');
			this.pkOrder = option.pkOrder;
			this.status = option.status;
			this.title = option.title;
			this.id = option.id;
			this.index = option.index;
		},
		
		methods: {
			// initScan() {
			// 	let _this = this;
			// 	main = plus.android.runtimeMainActivity(); //获取activity  
			// 	var IntentFilter = plus.android.importClass('android.content.IntentFilter');
			// 	filter = new IntentFilter();
			// 	filter.addAction("scan.rcv.message"); // 换你的广播动作  
			// 	receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
			// 		onReceive: function(context, intent) {
			// 			plus.android.importClass(intent);
			// 			let code = intent.getByteArrayExtra('barocode');
			// 		    let codeStr =utf8ByteToUnicodeStr(code);
			// 			scanDevice.stopScan();// 停止扫描	
			// 			_this.buttonScan(codeStr);
			// 		}
			// 	});				
			// },
			
			// registerScan() {
			// 	main.registerReceiver(receiver, filter);
			// },
			
			// unregisterScan() {
			// 	main.unregisterReceiver(receiver);
			// },
			
			queryData(){
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask:true
				});
				uni.request({
					url:this.baseUrl+'/system/overstorage/getInfo1',
					data:{
						id:this.pkOrder,
						orgId:this.orgId,
						userName:this.userName
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						console.log(res);
						//隐藏加载框
						uni.hideLoading();
						if(res.data.code == '200' || res.data.code == 200) {	
							this.productList = res.data.data;
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},			
				})
			},
			
			buttonScan(codeStr){
				if(this.userflag){
					return;
				}
				if(codeStr == null || codeStr == ''){
					uni.showToast({
						title: '请扫描数据',
						icon: 'error',
						duration: 2000
					});
					return;
				}
				if(codeStr.substring(0,1) == 'P' || codeStr.substring(0,1) == 'Q'){
					uni.showToast({
						title: '请扫描箱号',
						icon: 'error',
						duration: 2000
					});
					return;
				}else{	
					for(var j=0;j<this.productList.length;j++){
						if(this.productList[j].boxCode == codeStr){
							item.flag = true;
							break;
						}		
					}
				}
			},
			
			
			//生成越库单
			submit(){
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask:true
				});
				uni.request({
					url:this.baseUrl+'/system/overstorage/add2',
					data:{
						id:	this.id,
						operator:this.userName,
						orgId:this.orgId,
						sourcedoc:this.pkOrder
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{						
						if(res.data.code == '200' || res.data.code == 200) {	
							this.updateTask('3');					
						}else{
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},			
				})
			},
			
			refuse(){
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask:true
				});
				this.updateTask('4');
			},
			
			//更新代办任务状态为完成
			updateTask(symbol){
				var that = this;
				uni.request({
					url:that.baseUrl+'/system/wmsbacklog/update',
					data:{
						id:	that.id,
						executor:that.userName,
						orgId:that.orgId,
						startTime:'',
						endTime: new Date().toISOString().slice(0, 10),
						status:symbol//完成状态
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						if(res.data.code == '200' || res.data.code == 200) {	
							uni.showToast({
								title: '代办任务已完成',
								icon: 'none',
								duration: 3000
							});		
							//that.unregisterScan();
							setTimeout(function(){
								let pages = getCurrentPages();  //获取所有页面栈实例列表
								let nowPage = pages[ pages.length - 1];  //当前页页面实例
								let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							},1000);							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
						
					},			
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}
	.text-content {
		font-size: 48rpx;
		text-align: center;
	}
	
	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}
	
	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height:30px;
		font-size: 26rpx;
	}
	page {
		width: 100%;
		height: 100%;
	}
	
	
	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	
	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}
	
	.list-cont {
		flex: 1;
		position: relative;
	
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	
		}
	}
	
	
	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>
